package com.cxt.UI;


import com.cxt.BL.MonitorManifiestoBL;
import com.cxt.Bean.MonitorManifiestoBean;
import java.awt.AWTException;
import java.awt.Image;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import javax.swing.Timer;
import javax.swing.plaf.basic.BasicInternalFrameTitlePane;
import javax.swing.plaf.basic.BasicInternalFrameUI;
import javax.swing.table.DefaultTableModel;



public class FrmMain extends javax.swing.JFrame {
    

    /** Creates new form Base */
    public FrmMain() {
        initComponents();

        Image image = Toolkit.getDefaultToolkit().getImage("img/logo.png");
        
        Image imageTray = Toolkit.getDefaultToolkit().getImage("img/logo.png"); 
        
        trayIcon = new TrayIcon(imageTray, "CXT Transmition", null);
        
        setIconImage(image);
        
        ((BasicInternalFrameUI) intFrame.getUI()).setNorthPane(null);
        intFrame.setBorder(null);
        
        

    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        popupContextual = new javax.swing.JPopupMenu();
        menuItemRestore = new javax.swing.JMenuItem();
        separator = new javax.swing.JSeparator();
        menuItemSalir = new javax.swing.JMenuItem();
        pnlMenuPrincipal = new javax.swing.JPanel();
        btnMonitor = new javax.swing.JButton();
        intFrame = new javax.swing.JInternalFrame();

        menuItemRestore.setText("Restaurar");
        menuItemRestore.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuItemRestoreActionPerformed(evt);
            }
        });
        popupContextual.add(menuItemRestore);
        popupContextual.add(separator);

        menuItemSalir.setText("Salir");
        menuItemSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuItemSalirActionPerformed(evt);
            }
        });
        popupContextual.add(menuItemSalir);

        setTitle("CXT Transmition");
        setName("base"); // NOI18N
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                Closing(evt);
            }
        });

        pnlMenuPrincipal.setBackground(new java.awt.Color(255, 255, 255));

        btnMonitor.setText("Monitor");
        btnMonitor.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        btnMonitor.setOpaque(false);
        btnMonitor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMonitorActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlMenuPrincipalLayout = new javax.swing.GroupLayout(pnlMenuPrincipal);
        pnlMenuPrincipal.setLayout(pnlMenuPrincipalLayout);
        pnlMenuPrincipalLayout.setHorizontalGroup(
            pnlMenuPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlMenuPrincipalLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnMonitor, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                .addContainerGap())
        );
        pnlMenuPrincipalLayout.setVerticalGroup(
            pnlMenuPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlMenuPrincipalLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnMonitor, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(358, Short.MAX_VALUE))
        );

        intFrame.setBackground(new java.awt.Color(255, 255, 255));
        intFrame.setToolTipText("");
        intFrame.setVisible(true);

        javax.swing.GroupLayout intFrameLayout = new javax.swing.GroupLayout(intFrame.getContentPane());
        intFrame.getContentPane().setLayout(intFrameLayout);
        intFrameLayout.setHorizontalGroup(
            intFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 571, Short.MAX_VALUE)
        );
        intFrameLayout.setVerticalGroup(
            intFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnlMenuPrincipal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(intFrame)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(intFrame)
                    .addComponent(pnlMenuPrincipal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void menuItemRestoreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuItemRestoreActionPerformed
        this.setVisible(true);//GEN-LAST:event_menuItemRestoreActionPerformed
        this.toFront();
        tray.remove(trayIcon);
    }

    private void menuItemSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuItemSalirActionPerformed
        System.exit(0);
    }//GEN-LAST:event_menuItemSalirActionPerformed

    private void Closing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_Closing
        alTray();
    }//GEN-LAST:event_Closing

    private void btnMonitorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMonitorActionPerformed
        FrmTblMonitorManifiesto frmMonitor = new FrmTblMonitorManifiesto();
        
        intFrame.setContentPane(frmMonitor.getContentPane());
        intFrame.pack();
        
    }//GEN-LAST:event_btnMonitorActionPerformed
    
    private void alTray(){
        if (SystemTray.isSupported()) {
                
                tray = SystemTray.getSystemTray();

                MouseListener mouseListener = new MouseListener( ) {

                    public void mouseClicked(MouseEvent e) {
                        if ( e.getButton()==e.BUTTON1 ) {
                            trayIcon.displayMessage("CXT", "Transmition running",TrayIcon.MessageType.INFO);
                        }
                    }

                    public void mouseEntered(MouseEvent e) {

                    }

                    public void mouseExited(MouseEvent e) {

                    }

                    public void mousePressed(MouseEvent e) {
                        
                    }

                    public void mouseReleased(MouseEvent e) {
                        if (e.isPopupTrigger()) {
                            popupContextual.setLocation(e.getX(), e.getY());
                            popupContextual.setInvoker(popupContextual);
                            popupContextual.setVisible(true);
                        }                    
                    }
                };

                trayIcon.setImageAutoSize(true);
                trayIcon.addMouseListener(mouseListener);
                trayIcon.addActionListener( new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                            menuItemRestoreActionPerformed(e);
                        }
                    }
                );


                try {
                    tray.add(trayIcon);
                } catch (AWTException e) {
                    System.err.println("No se pudo agregar el ícono a la barra tray");
                    this.setVisible(true);
                }
            } else {
                //  System Tray is not supported
            }
    }
    
    
    
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new FrmMain().setVisible(true);
            }
        });
        
       
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnMonitor;
    private javax.swing.JInternalFrame intFrame;
    private javax.swing.JMenuItem menuItemRestore;
    private javax.swing.JMenuItem menuItemSalir;
    private javax.swing.JPanel pnlMenuPrincipal;
    private javax.swing.JPopupMenu popupContextual;
    private javax.swing.JSeparator separator;
    // End of variables declaration//GEN-END:variables

    private SystemTray          tray;
    private final TrayIcon      trayIcon;


    


}
